package com.fone.player.activity.personal;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.TypedValue;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.astuetz.PagerSlidingTabStrip;
import com.fone.player.ApplicationManage;
import com.fone.player.R;
import com.fone.player.activity.BaseActivity;
import com.fone.player.activity.main.MainTabActivity;
import com.fone.player.activity.main.SplashActivity;
import com.fone.player.activity.personal.adapter.MessageAdapter;
import com.fone.player.activity.personal.adapter.NotificationAdapter;
import com.fone.player.activity.personal.adapter.NotifyPagerAdapter;
import com.fone.player.client.Reporter;
import com.fone.player.client.Request;
import com.fone.player.entity.Download;
import com.fone.player.entity.NotificationBean;
import com.fone.player.play.PlayerAction;
import com.fone.player.play.PlayerFrom;
import com.fone.player.play.WebPlayerFrom;
import com.fone.player.service.NotificationReceiver;
import com.fone.player.storage.SharedPreferenceModule;
import com.fone.player.storage.StorageModule;
import com.fone.player.util.ActivityQueue;
import com.fone.player.util.FoneUtil;
import com.fone.player.util.L;
import com.fone.player.util.Notice2Online;
import com.fone.player.util.ScreenUtil;
import com.fone.player.widget.xlistview.XListView;
import com.fone.player.widget.xlistview.XListView.IXListViewListener;

/**
 * 通知模块
 * 
 * @author Jason_Zue
 * 
 */
public class NotificationActivity extends BaseActivity implements
		IXListViewListener, OnClickListener {

	private Context mContext;
	private static String TAG = "NotificationActivity";
	private TextView mTitleTv;
	private Button mLeftBt, mRightBt;

	private LayoutInflater lf;
	private View notification_view, message_view;
	private List<View> viewList = new ArrayList<View>();
	private List<String> mTitleData = new ArrayList<String>();

	private List<NotificationBean> mNotificationList, mMessageList;
	private XListView NotificationListView, MessageListView;
	private NotificationAdapter mNotificationAdapter;
	private MessageAdapter mMessageAdapter;
	private NotifyPagerAdapter pagerAdapter;
	private ViewPager mViewPager;
	private PagerSlidingTabStrip tabs;
	private ImageView newMessageIcon;

	private LinearLayout mDeleteView;
	private Button mSelectAllBt, mDeleteBt;

	public List<Boolean> deleteModeList = new ArrayList<Boolean>();// 该数据用来修改界面显示checkbox与否,与选中数量无关!!

	public int mViewMode = NORMAL_MODE;

	public static boolean isManageMode = false;
	public static final int NORMAL_MODE = 1; // 普通界面模式
	public static final int DELETE_MODE = 2;// 管理界面模式
	// 通知页面 消息页面
	private static final int NOTIFICATION_VIEW = 0, MESSAGE_VIEW = 1;
	// 当前页面
	private int mCurrentViewID = NOTIFICATION_VIEW;
	private boolean hasSelectedAll;// 是否全部选中删除
	
	private boolean isfromNotification;
	private int notificationType;

	// 链接类型
	private static final int FULLSCREEN = 0; // 全屏页
	private static final int DETAILVIEW = 1; // 详情页
	@SuppressWarnings("unused")
	private static final int POSTERVIEW = 2; //
	private static final int WAP = 3; // 打开wap网页
	private static final int DOWNLOADSOFT = 4; // 下载应用
	private static final int FEEPAGE = 5; // 计费页面
	// private static final int FEEDBACK = 6; // 评论列表
	private static final int PROGRAM = 7; // 栏目页
	private static final int WEBVIEW = 8;// 打开原始网页
	private static final int FEEDBACK = 9; // 反馈回复
	@SuppressWarnings("unused")
	private static final int NOTIFITIONVIEW = 10; // 消息列表
	// 数据上报
//	public static final int NOTIC_INNER_CLICK = 0x9000000b; /* 内通知点击次数 */
//	public static final int CLICK_NOTIC_BTN = 0xa0000017; /* “通知”入口 */
//	public static final int CLICK_MSG_BTN = 0xa0000019; /* “消息”入口 */

	// 通知类型
	@SuppressWarnings("unused")
	private static final int FEEDBACKTYPE = 6; // 评论列表

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.personal_notification_view);
		mContext = getApplicationContext();
		// 初始化默认模式
		deleteModeList.add(isManageMode);
		initView();
		initData();
	}

	private Animation mShowFromButtomAction;

	private Animation mHiddenButtomAction;

	private MyPagerChangeListener mPagerListener;
	private NotificationItemListener mNotificationListener;
	private NotificationItemListener mMessageListener;

	@SuppressWarnings("static-access")
	private void initView() {

		mLeftBt = (Button) findViewById(R.id.common_title_left_bt);
		mRightBt = (Button) findViewById(R.id.common_title_right_bt);
		mTitleTv = (TextView) findViewById(R.id.common_title_text_tv);
		mDeleteView = (LinearLayout) findViewById(R.id.notify_delete_ll);
		mSelectAllBt = (Button) findViewById(R.id.notify_select_all_bt);
		mDeleteBt = (Button) findViewById(R.id.notify_delete_bt);
		newMessageIcon = (ImageView) findViewById(R.id.iv_new_message_icon);

		mLeftBt.setText("消息通知");
		mRightBt.setText("删除");
		mTitleTv.setVisibility(View.GONE);
		mRightBt.setOnClickListener(this);
		mLeftBt.setOnClickListener(this);
		mSelectAllBt.setOnClickListener(this);
		mDeleteBt.setOnClickListener(this);

		lf = getLayoutInflater().from(mContext);

		notification_view = lf.inflate(R.layout.personal_notify_viewpager_item,
				null);
		message_view = lf
				.inflate(R.layout.personal_notify_viewpager_item, null);

		viewList.add(notification_view);
		viewList.add(message_view);

		mViewPager = (ViewPager) findViewById(R.id.cache_viewpager);

		initNotifyTitle();
		pagerAdapter = new NotifyPagerAdapter(viewList, mTitleData);
		mViewPager.setAdapter(pagerAdapter);

		tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
		tabs.setTypeface(null, 0);
		tabs.setViewPager(mViewPager);
		mPagerListener = new MyPagerChangeListener();
		tabs.setOnPageChangeListener(mPagerListener);
		// 设置pager头选中颜色及宽度属性
		tabs.setTabWidthPx(getResources().getDisplayMetrics().widthPixels / 2);
		int pageMargin = (int) TypedValue.applyDimension(
				TypedValue.COMPLEX_UNIT_DIP, 2, getResources()
						.getDisplayMetrics());
		mViewPager.setPageMargin(pageMargin);
		tabs.setTextSize(ScreenUtil.dp2px(15));
		tabs.setTextSelectedColor(getResources().getColor(
				R.color.video_info_tab_line));

		// pager中对应页面的控件
		NotificationListView = (XListView) notification_view
				.findViewById(R.id.pager_item_lv);
		MessageListView = (XListView) message_view
				.findViewById(R.id.pager_item_lv);
		mNotificationList = new ArrayList<NotificationBean>();
		mMessageList = new ArrayList<NotificationBean>();

		mNotificationListener = new NotificationItemListener(mNotificationList);
		mMessageListener = new NotificationItemListener(mMessageList);
		NotificationListView.setOnItemClickListener(mNotificationListener);
		MessageListView.setOnItemClickListener(mMessageListener);

		mNotificationAdapter = new NotificationAdapter(mContext,
				mNotificationList, deleteModeList);
		NotificationListView.setAdapter(mNotificationAdapter);

		mMessageAdapter = new MessageAdapter(mContext, mMessageList,
				deleteModeList);
		MessageListView.setAdapter(mMessageAdapter);

		NotificationListView.setPullRefreshEnable(false);
		NotificationListView.setPullLoadEnable(true);
		NotificationListView.setEnableDragLoadMore(true);
		NotificationListView.setXListViewListener(this);

		MessageListView.setPullRefreshEnable(false);
		MessageListView.setPullLoadEnable(true);
		MessageListView.setEnableDragLoadMore(true);
		MessageListView.setXListViewListener(this);
		pagerAdapter.notifyDataSetChanged();
		initAnimation();
	}

	/**
	 * 初始化title数据
	 * 
	 * @return void
	 * @throws
	 */
	private void initNotifyTitle() {
		mTitleData.removeAll(mTitleData);
		mTitleData.add("通知");
		mTitleData.add("消息");

	}

	// 获取数据
	private void initData() {
		Intent intent = getIntent();
		isfromNotification = intent.getBooleanExtra(
				NotificationReceiver.FROMNOTIFICATION, false);
		L.v(TAG, " initData isfromNotification : " + isfromNotification);
		if(isfromNotification){
			notificationType = intent.getIntExtra(NotificationReceiver.NOTIFICATIONTYPE,
					NotificationBean.NOTIFICATION_TYPE);
			L.v(TAG, " initData notificationType : " + notificationType);
			if(notificationType==NotificationBean.INFORMATION_TYPE){
				mViewPager.setCurrentItem(1);
				newMessageIcon.setVisibility(View.GONE);
			}
		}
		
		mNotificationList.clear();
		mMessageList.clear();
		// 通知类型
		List<NotificationBean> notifications = StorageModule.getInstance()
				.getNotificationList(true, NotificationBean.NOTIFICATION_TYPE,
						0, 10);// 传入true,表示是刷新（或者去最新的数据）
		if(notifications!=null){
			mNotificationList.addAll(notifications);
		}
		List<NotificationBean> messages = StorageModule.getInstance()
				.getNotificationList(true, NotificationBean.INFORMATION_TYPE,
						0, 10);
		if(messages!=null){
			mMessageList.addAll(messages);
		}
		mNotificationAdapter.notifyDataSetChanged();
		mMessageAdapter.notifyDataSetChanged();
		if (mNotificationList.size() < 10) {
			NotificationListView.setPullLoadEnable(false);
			NotificationListView.setEnableDragLoadMore(false);
		} else {
			NotificationListView.setPullLoadEnable(true);
			NotificationListView.setEnableDragLoadMore(true);
		}
		if (mMessageList.size() < 10) {
			MessageListView.setPullLoadEnable(false);
			MessageListView.setEnableDragLoadMore(false);
		} else {
			MessageListView.setPullLoadEnable(true);
			MessageListView.setEnableDragLoadMore(true);
		}
		initTitleDeleteBtn();
		
		boolean hasNewNotification = SharedPreferenceModule.getInstance()
				.getBoolean(MainTabActivity.NEWNOTIFICATION, false);
		
		if(mMessageList!=null && mMessageList.size()>0
				&& !mMessageList.get(0).getNotificationIsRead() && hasNewNotification){
			newMessageIcon.setVisibility(View.VISIBLE);
		}
	}
	
	/**
	 * 初始化监听器
	 */
//	private void iniListener(){
//		NotificationListView.setXListViewListener(new IXListViewListener() {
//			@Override
//			public void onRefresh() {}
//			
//			@Override
//			public void onLoadMore() {
//				if (mNotificationList.size() > 0) {
//					NotificationBean bean = mNotificationList.get(mNotificationList
//							.size() - 1);
//					List<NotificationBean> list = StorageModule.getInstance()
//							.getNotificationList(false,
//									NotificationBean.NOTIFICATION_TYPE,
//									bean.getNotificationId(), 10);// 传入false,表示不是刷新，而是加载更多
//					if (list != null && list.size() > 0) {
//						
//						if(list.size()<10){
//							MessageListView.setPullLoadEnable(false);
//							MessageListView.setEnableDragLoadMore(false);
//						}
//						
//						mNotificationList.addAll(list);
//						mNotificationAdapter.notifyDataSetChanged();
//					} else {
//						MessageListView.setPullLoadEnable(false);
//						MessageListView.setEnableDragLoadMore(false);
//						FoneUtil.showToast(mContext, "已经到最后一页了");
//					}
//				}
//			}
//		});
//		MessageListView.setXListViewListener(new IXListViewListener() {
//			
//			@Override
//			public void onRefresh() {}
//			
//			@Override
//			public void onLoadMore() {
//				
//				if (mMessageList.size() > 0) {
//					NotificationBean bean = mMessageList.get(mMessageList
//							.size() - 1);
//					List<NotificationBean> list = StorageModule.getInstance()
//							.getNotificationList(false,
//									NotificationBean.NOTIFICATION_TYPE,
//									bean.getNotificationId(), 10);// 传入false,表示不是刷新，而是加载更多
//					if (list != null && list.size() > 0) {
//						
//						if(list.size()<10){
//							MessageListView.setPullLoadEnable(false);
//							MessageListView.setEnableDragLoadMore(false);
//						}
//						
//						mMessageList.addAll(list);
//						mMessageAdapter.notifyDataSetChanged();
//					} else {
//						MessageListView.setPullLoadEnable(false);
//						MessageListView.setEnableDragLoadMore(false);
//						FoneUtil.showToast(mContext, "已经到最后一页了");
//					}
//				}
//				
//			}
//		});
//		
//	}

	// 设置头部删除按钮是否可点击
	private void initTitleDeleteBtn() {
		int color = mContext.getResources().getColor(
				R.color.notification_title_delete_color);
		if (mCurrentViewID == NOTIFICATION_VIEW) {
			if (mNotificationList.size() > 0) {
				mRightBt.setClickable(true);
				mRightBt.setOnClickListener(this);
				mRightBt.setTextColor(color);
			} else {
				mRightBt.setClickable(false);
				mRightBt.setOnClickListener(null);
				mRightBt.setTextColor(Color.GRAY);
			}

		} else if (mCurrentViewID == MESSAGE_VIEW) {
			if (mMessageList.size() > 0) {
				mRightBt.setClickable(true);
				mRightBt.setOnClickListener(this);
				mRightBt.setTextColor(color);
			} else {
				mRightBt.setClickable(false);
				mRightBt.setOnClickListener(null);
				mRightBt.setTextColor(Color.GRAY);
			}
		}
	}

	/**
	 * 刷新数据
	 */
	public void refreshData(int type) {
		// 消息类型
		if (type == NotificationBean.INFORMATION_TYPE) {
			List<NotificationBean> list = null;
			if (mMessageList.size() > 0) {
				NotificationBean bean = mMessageList.get(0);
				list = StorageModule.getInstance()
						.getNotificationList(true,
								NotificationBean.INFORMATION_TYPE,
								bean.getNotificationId(), 10);
				if(list!=null){
					mMessageList.addAll(0, list);
				}
			} else {
				list = StorageModule.getInstance()
						.getNotificationList(true,
								NotificationBean.INFORMATION_TYPE, 0, 10);
				if(list!=null){
					mMessageList.addAll(0, list);
				}
			}
//			if (mMessageList.size() == 0|| list == null ||list.size()<10) {
//				MessageListView.setPullLoadEnable(false);
//				MessageListView.setEnableDragLoadMore(false);
//			} else {
//				MessageListView.setPullLoadEnable(true);
//				MessageListView.setEnableDragLoadMore(true);
//			}
			mMessageAdapter.notifyDataSetChanged();
		} else if (type == NotificationBean.NOTIFICATION_TYPE) {// 通知类型
			List<NotificationBean> list = null;
			if (mNotificationList.size() > 0) {
				NotificationBean bean = mNotificationList.get(0);
				list = StorageModule.getInstance()
						.getNotificationList(true,
								NotificationBean.NOTIFICATION_TYPE,
								bean.getNotificationId(), 10);
				if(list!=null){
					mNotificationList.addAll(0, list);
				}

			} else {
				list = StorageModule.getInstance()
						.getNotificationList(true,
								NotificationBean.NOTIFICATION_TYPE, 0, 10);
				if(list!=null){
					mNotificationList.addAll(0, list);
				}
			}
//			if (mNotificationList.size() == 0|| list == null  || list.size()<10) {
//				NotificationListView.setPullLoadEnable(false);
//				NotificationListView.setEnableDragLoadMore(false);
//			} else {
//				NotificationListView.setPullLoadEnable(true);
//				NotificationListView.setEnableDragLoadMore(true);
//			}
			mNotificationAdapter.notifyDataSetChanged();
		}
		initTitleDeleteBtn();

	}

	@Override
	public void onRefresh() {

	}

	@Override
	public void onLoadMore() {
		if (mCurrentViewID == NOTIFICATION_VIEW) {
			if (mNotificationList.size() > 0) {
				NotificationBean bean = mNotificationList.get(mNotificationList
						.size() - 1);
				List<NotificationBean> list = StorageModule.getInstance()
						.getNotificationList(false,
								NotificationBean.NOTIFICATION_TYPE,
								bean.getNotificationId(), 10);// 传入false,表示不是刷新，而是加载更多
				MessageListView.stopLoadMore();
				if (list != null && list.size() > 0) {
					
					if(list.size()<10){
						MessageListView.setPullLoadEnable(false);
						MessageListView.setEnableDragLoadMore(false);
					}
					mNotificationList.addAll(list);
					mNotificationAdapter.notifyDataSetChanged();
				} else {
					MessageListView.setPullLoadEnable(false);
					MessageListView.setEnableDragLoadMore(false);
					FoneUtil.showToast(mContext, "已经到最后一页了");
				}
			}
		} else {
			if (mCurrentViewID == MESSAGE_VIEW) {
				if (mMessageList.size() > 0) {
					NotificationBean bean = mMessageList.get(mMessageList
							.size() - 1);
					List<NotificationBean> list = StorageModule.getInstance()
							.getNotificationList(false,
									NotificationBean.INFORMATION_TYPE,
									bean.getNotificationId(), 10);// 传入false,表示不是刷新，而是加载更多
					MessageListView.stopLoadMore();
					if (list != null && list.size() > 0) {
						if(list.size()<10){
							MessageListView.setPullLoadEnable(false);
							MessageListView.setEnableDragLoadMore(false);
						}
						
						mMessageList.addAll(list);
						mMessageAdapter.notifyDataSetChanged();
					} else {
						MessageListView.setPullLoadEnable(false);
						MessageListView.setEnableDragLoadMore(false);
						FoneUtil.showToast(mContext, "已经到最后一页了");
					}
				}
			}
		}
		initTitleDeleteBtn();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.common_title_right_bt:// 头部删除按钮
			if (mCurrentViewID == NOTIFICATION_VIEW) {
				if (mViewMode == NORMAL_MODE) {
					mViewMode = DELETE_MODE;
					changeNotificationViewMode(DELETE_MODE);
				} else if (mViewMode == DELETE_MODE) {
					mViewMode = NORMAL_MODE;
					changeNotificationViewMode(NORMAL_MODE);
				}
			} else if (mCurrentViewID == MESSAGE_VIEW) {
				if (mViewMode == NORMAL_MODE) {
					mViewMode = DELETE_MODE;
					changeMessageViewMode(DELETE_MODE);
				} else if (mViewMode == DELETE_MODE) {
					mViewMode = NORMAL_MODE;
					changeMessageViewMode(NORMAL_MODE);
				}
			}
			break;
		case R.id.common_title_left_bt:
			finish();
			break;
		case R.id.notify_select_all_bt:// 全选
			hasSelectedAll = !hasSelectedAll;
			L.i(TAG, "hasSelectedAll:" + hasSelectedAll);
			if (mCurrentViewID == NOTIFICATION_VIEW) {
				doSelectAll(mCurrentViewID, mNotificationList);
			} else {
				doSelectAll(mCurrentViewID, mMessageList);
			}
			break;
		case R.id.notify_delete_bt:
			// 移除的时候不可以用++ 否则数据会污染
			if (mCurrentViewID == NOTIFICATION_VIEW) {
				doDelete(mCurrentViewID, mNotificationList);
			} else {
				doDelete(mCurrentViewID, mMessageList);
			}
			initTitleDeleteBtn();
			break;

		default:
			break;
		}
	}

	// 进行删除
	private void doDelete(int ViewID, List<NotificationBean> list) {
		int count = 0;
		List<NotificationBean> deleteList = new ArrayList<NotificationBean>();
		Iterator<NotificationBean> it = list.iterator();
		while (it.hasNext()) {
			NotificationBean bean = it.next();
			if (bean.isDeleteSelected()) {
				count++;
				deleteList.add(bean);
				it.remove();
			}
		}

		if (count <= 0) {
			FoneUtil.showToast(mContext, "没有选中的内容");
			return;
		} else {
			int result = StorageModule.getInstance().deleteNotificationById(
					deleteList);
			L.i(TAG, "doDelete", "result:" + result);
		}
		mSelectAllBt.setText("全选");
		mDeleteBt.setText("删除");
		mDeleteBt.setTextColor(getResources().getColor(
				R.color.btn_unpress_text_color));
		if (mCurrentViewID == NOTIFICATION_VIEW) {
			mNotificationAdapter.notifyDataSetChanged();
		} else {
			mMessageAdapter.notifyDataSetChanged();
		}

	}

	public void doSelectAll(int ViewId, List<NotificationBean> list) {
		for (int i = 0; i < list.size(); i++) {
			if (!hasSelectedAll) {// 处于未全选状态
				L.i(TAG, "全部未选中！");
				list.get(i).setDeleteSelected(false);
				mSelectAllBt.setTextColor(getResources().getColor(
						R.color.btn_unpress_text_color));
				mDeleteBt.setText("删除");
				mDeleteBt.setTextColor(getResources().getColor(
						R.color.btn_unpress_text_color));
				mSelectAllBt.setText("全选");
			} else {// 处于全选状态
				L.i(TAG, "全部选中！");
				mDeleteBt.setTextColor(getResources().getColor(
						R.color.btn_pressed_text_color));
				list.get(i).setDeleteSelected(true);
				mDeleteBt.setText("删除(" + list.size() + ")");
				mSelectAllBt.setText("取消全选");
			}
		}
		if (mCurrentViewID == NOTIFICATION_VIEW) {
			mNotificationAdapter.notifyDataSetChanged();
		} else {
			mMessageAdapter.notifyDataSetChanged();
		}
	}

	// 改变通知页面的当前模式
	private void changeNotificationViewMode(int ViewMode) {
		if (ViewMode == DELETE_MODE) {
			hasSelectedAll = false;
			deleteModeList.set(0, true);
			mRightBt.setText("取消");
			mDeleteView.setVisibility(View.VISIBLE);
			mDeleteView.startAnimation(mShowFromButtomAction);
			mNotificationAdapter.notifyDataSetChanged();
		} else if (ViewMode == NORMAL_MODE) {
			deleteModeList.set(0, false);
			mRightBt.setText("删除");
			if (mDeleteView.getVisibility() != View.GONE) {
				mDeleteView.startAnimation(mHiddenButtomAction);
			}
			mNotificationAdapter.notifyDataSetChanged();
			// 点击取消需要清除当前记录的选中信息
			for (int i = 0; i < mNotificationList.size(); i++) {
				mNotificationList.get(i).setDeleteSelected(false);
			}
			mDeleteBt.setText("删除");
			mDeleteBt.setTextColor(mContext.getResources().getColor(
					R.color.btn_unpress_text_color));
		}
	}

	// 改变消息页面的当前模式
	private void changeMessageViewMode(int ViewMode) {
		if (ViewMode == DELETE_MODE) {
			hasSelectedAll = false;
			deleteModeList.set(0, true);
			mRightBt.setText("取消");
			mDeleteView.startAnimation(mShowFromButtomAction);
			mMessageAdapter.notifyDataSetChanged();
		} else if (ViewMode == NORMAL_MODE) {
			deleteModeList.set(0, false);
			mRightBt.setText("删除");
			if (mDeleteView.getVisibility() != View.GONE) {
				mDeleteView.startAnimation(mHiddenButtomAction);
			}
			mMessageAdapter.notifyDataSetChanged();
			// 点击取消需要清除当前记录的选中信息
			for (int i = 0; i < mMessageList.size(); i++) {
				mMessageList.get(i).setDeleteSelected(false);
			}
			mDeleteBt.setText("删除");
			mDeleteBt.setTextColor(mContext.getResources().getColor(
					R.color.btn_unpress_text_color));
		}
	}

	public void initAnimation() {
		mShowFromButtomAction = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
				0.0f, Animation.RELATIVE_TO_SELF, 1.0f,
				Animation.RELATIVE_TO_SELF, 0.0f);
		mShowFromButtomAction.setDuration(200);

		mHiddenButtomAction = new TranslateAnimation(
				Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
				0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
				Animation.RELATIVE_TO_SELF, 1.0f);
		mHiddenButtomAction.setDuration(200);
		mHiddenButtomAction.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				mDeleteView.setVisibility(View.VISIBLE);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				mDeleteView.setVisibility(View.GONE);

			}
		});
		mShowFromButtomAction.setAnimationListener(new AnimationListener() {

			@Override
			public void onAnimationStart(Animation animation) {
				mDeleteView.setVisibility(View.VISIBLE);
			}

			@Override
			public void onAnimationRepeat(Animation animation) {

			}

			@Override
			public void onAnimationEnd(Animation animation) {
				mDeleteView.setVisibility(View.VISIBLE);

			}
		});
	}

	private class MyPagerChangeListener implements OnPageChangeListener {
		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		@Override
		public void onPageSelected(int arg0) {
			mCurrentViewID = arg0;
			mViewMode = NORMAL_MODE;
			// 将上个页面恢复到正常模式
			if (mCurrentViewID == NOTIFICATION_VIEW) {
				Reporter.logEvent(Reporter.EventId.CLICK_NOTIC_BTN);
				changeMessageViewMode(mViewMode);

			} else {
				
				if(newMessageIcon.getVisibility() == View.VISIBLE){
					newMessageIcon.setVisibility(View.GONE);
				}
				Reporter.logEvent(Reporter.EventId.CLICK_MSG_BTN);
				changeNotificationViewMode(mViewMode);
			}
			initTitleDeleteBtn();
			
		}
	}

	private class NotificationItemListener implements OnItemClickListener {
		private List<NotificationBean> list;

		public NotificationItemListener(List<NotificationBean> list) {
			this.list = list;
		}

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			CheckBox mBox = (CheckBox) view
					.findViewById(R.id.notify_is_selected_cb);
			NotificationBean bean = list.get(position - 1);
			if (mBox.getVisibility() == View.VISIBLE) {
				bean.setDeleteSelected(!bean.isDeleteSelected());
				L.i(TAG, "NotificationItemListener", "bean:" + bean.toString());
				mBox.setChecked(bean.isDeleteSelected());
				int count = 0;
				for (int i = 0; i < list.size(); i++) {
					if (list.get(i).isDeleteSelected()) {
						count++;
					}
				}
				if (count > 0) {
					if (count == mNotificationList.size()) {
						hasSelectedAll = true;
						mSelectAllBt.setText("取消全选");
					} else {
						hasSelectedAll = false;
						mSelectAllBt.setText("全选");
					}
					mDeleteBt.setText("删除(" + count + ")");
					mDeleteBt.setTextColor(getResources().getColor(
							R.color.btn_pressed_text_color));
				} else {
					hasSelectedAll = false;
					mSelectAllBt.setText("全选");
					mDeleteBt.setText("删除");
					mDeleteBt.setTextColor(getResources().getColor(
							R.color.btn_unpress_text_color));
				}
			} else if (mBox.getVisibility() == View.GONE) {
				// 数据上报
				Reporter.logEvent(Reporter.EventId.NOTIC_INNER_CLICK);
				// 更改阅读状态
				boolean isRead = bean.getNotificationIsRead();
				if (isRead == false) {
					bean.setNotificationIsRead(true);
					StorageModule.getInstance().updateNotificationIsRead(bean);
					if (mCurrentViewID == NOTIFICATION_VIEW) {
						mNotificationAdapter.notifyDataSetChanged();
					} else if (mCurrentViewID == MESSAGE_VIEW) {
						mMessageAdapter.notifyDataSetChanged();
					}
				}
				// 进行跳转到别的页面
				change2OtherView(bean);
			}
		}

		// 点击条目后进行跳转
		private void change2OtherView(NotificationBean notificationBean) {
			// 根据连接类型 分类不同广播
			int jumpType = notificationBean.getNotificationExternalType();
			L.v(TAG, "Notification", " jumpType : " + jumpType);
			switch (jumpType) {
			case FULLSCREEN:
				// 全屏播放
				L.v(TAG, "Notification switch ", " ---> FULLSCREEN");
				FoneUtil.openOnlineVideo(NotificationActivity.this, PlayerFrom.NOTIFICATION_INTERNAL,
						PlayerAction.FULLPLAY, null,
						notificationBean.getNotificationUrl(),
						notificationBean.getNotificationDefinitionType(), null);
				break;
			case DETAILVIEW:
				// 详情页
				L.v(TAG, "Notification switch ", " ---> DETAILVIEW");
				FoneUtil.openOnlineVideo(NotificationActivity.this, PlayerFrom.NOTIFICATION_INTERNAL,
						PlayerAction.DETAIL,
						notificationBean.getNotificationUrl(), null,
						notificationBean.getNotificationDefinitionType(), null);
				break;
			case WAP:
				// TODO 打开wap网页
				L.v(TAG, "Notification switch ", " ---> WAP");
				// 跳转到wap
				String webUrl = notificationBean.getNotificationUrl();
				String columnName = notificationBean.getNotificationName();
				String cid2web = notificationBean.getNotificationServerId()
												.substring(0, notificationBean.getNotificationServerId().indexOf("_"));
				Notice2Online.startActivity(NotificationActivity.this, false, webUrl, columnName,cid2web,-1);
				break;
			case DOWNLOADSOFT:
				// 软件推荐
				L.v(TAG, "Notification switch ", " ---> DOWNLOADSOFT");
				// 下载
				String downloadUrl = notificationBean.getNotificationUrl()+"&cipher="+Request.getInstance().getCipher();
				String downloadTitle = notificationBean.getNotificationName();
				
				L.v(TAG, "DOWNLOADSOFT", " downloadUrl : " + downloadUrl);
				Download download = new Download();
				download.setDownloadIsShowRunningNotification(true);
				download.setDownloadUrl(downloadUrl);
				download.setDownloadFileName(downloadTitle);
				download.setDownloadNotification(ApplicationManage.getGlobalContext());
				download.setDownloadIsInstall(true);
				download.setDownloadIsErrorToast(true);
				download.setDownloadIsLimitSpeed(false);
				download.setDownloadType(Download.DOWNLOAD_RECOMMEND);
				StorageModule.getInstance().addFileDownload(download);
				
				break;
			case FEEPAGE:
				// TODO 打开计费
				L.v(TAG, "Notification switch ", " ---> FEEPAGE");
				break;
			case PROGRAM:
				// 栏目页
				L.v(TAG, "Notification switch ", " ---> PROGRAM");
				// 跳转到栏目页面
				String columnUrl = notificationBean.getNotificationUrl();
				String Name = notificationBean.getNotificationName();
				String cid2program = notificationBean.getNotificationServerId()
													 .substring(0, notificationBean.getNotificationServerId().indexOf("_"));
				Notice2Online.startActivity(NotificationActivity.this, true, columnUrl, Name,cid2program,-1);
				break;
			case WEBVIEW:
				// 视频原始网页
				L.v(TAG, "Notification switch ", " ---> WEBVIEW");
				FoneUtil.open3rdPlayer(NotificationActivity.this,
						WebPlayerFrom.NOTIFICATION_IN, notificationBean
								.getNotificationExternalUrl(), notificationBean
								.getNotificationUrl(), null, notificationBean
								.getNotificationIsShowPlayButton() ? 1 : 0,notificationBean.getNotificationShareUrl(),notificationBean.getNotificationImageUrl());
				break;
			case FEEDBACK:
				// 反馈回复页
				L.v(TAG, "Notification switch ", " ---> FEEDBACK");
				Intent notificationIntent = new Intent(mContext,
						NotifyFeedBackActivity.class);
				notificationIntent.putExtra(
						NotificationReceiver.NOTIFICATION_INFO,
						notificationBean);
				startActivity(notificationIntent);
				break;

			// case NOTIFITIONVIEW:
			// // 通知页面
			// L.v(TAG, "sendNotification switch ", " ---> NOTIFITIONVIEW");
			// notificationIntent
			// .setAction(NotificationReceiver.NOTIFICATION_NOTIFICATIONVIEW);
			// notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			// break;

			default:
				// 默认通知 点击后不做跳转
				L.v(TAG, "Notification switch ", " ---> other ");
				break;
			}
		}
	}

	@Override
	public void finish() {
		// 未启动应用的通知过来 要打开应用
		if (isfromNotification && ActivityQueue.getSize() <= 1) {
			Intent i = new Intent(this, SplashActivity.class);
			startActivity(i);
		}
		super.finish();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		mViewPager.removeAllViewsInLayout();
		NotificationListView.removeAllViewsInLayout();
		MessageListView.removeAllViewsInLayout();
		pagerAdapter.removeData();
		pagerAdapter = null;
		viewList.clear();
		viewList = null;
		System.gc();
		
		
//		if(mNotificationList!=null && mNotificationList.size()>0){
//			StorageModule.getInstance().updateNotificationIsShow(mNotificationList);
//		}
//		if(mMessageList!=null && mMessageList.size()>0){
//			StorageModule.getInstance().updateNotificationIsShow(mMessageList);
//		}
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && mViewMode == DELETE_MODE) {
			mViewMode = NORMAL_MODE;
			if (mCurrentViewID == NOTIFICATION_VIEW) {
				changeNotificationViewMode(mViewMode);
			} else {
				changeMessageViewMode(mViewMode);
			}
			return true;
		} else {
			return super.onKeyDown(keyCode, event);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		// 退出页面时，将标记改为目前已经没有新的通知消息了（是为了修改我的视频页面的通知icon的标记）
		SharedPreferenceModule.getInstance().setBoolean(
				MainTabActivity.NEWNOTIFICATION, false);
	}
}
